﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Criterion;
using FJASPX.Framwork.Query.Util;
using System.Collections;

namespace FJASPX.Framwork.Query
{
    /// <summary>
    /// 约束模板
    /// </summary>
    [Serializable]
    public class RestrictionTemplate
    {
        private bool isLike;
        private MatchMode matchMode;
        public RestrictionTemplate(bool isLike)
        {
            this.isLike = isLike;
            this.matchMode = MatchMode.Anywhere;
        }
        public RestrictionTemplate(bool isLike, MatchMode matchMode)
        {
            this.isLike = isLike;
            this.matchMode = matchMode;
        }
        public void setMatchMode(MatchMode matchMode)
        {
            this.matchMode = matchMode;
        }
        public void setMatchMode(String matchMode)
        {
            if (MatchMode.Anywhere.ToString().Equals(matchMode))
                this.matchMode = MatchMode.Anywhere;
            else if (MatchMode.Exact.ToString().Equals(matchMode))
                this.matchMode = MatchMode.Exact;
            else if (MatchMode.Start.ToString().Equals(matchMode))
                this.matchMode = MatchMode.Start;
            else if (MatchMode.End.ToString().Equals(matchMode))
                this.matchMode = MatchMode.End;
            else
                throw new Exception("Invalid match mode: " + matchMode);
        }
        public AbstractCriterion IdEq(object value)
        {
            if (ObjectUtil.IsNotEmpty(value))
            {
                return Restrictions.IdEq(value);
            }
            return null;
        }

        public AbstractCriterion AllEq(Dictionary<object, object> dic)
        {

            if (ObjectUtil.IsNotEmpty(dic))
            {
                return Restrictions.AllEq(dic);
            }
            return null;
        }

        public SimpleExpression Eq(string propertyName, object value)
        {
            if (ObjectUtil.IsNotEmpty(value))
            {
                return Restrictions.Eq(propertyName, value);
            }
            return null;

        }

        public AbstractCriterion EqProperty(string propertyName1, string propertyName2)
        {
            return Restrictions.EqProperty(propertyName1, propertyName2);
        }
        public AbstractCriterion NotEqProperty(string propertyName1, string propertyName2)
        {
            return Restrictions.NotEqProperty(propertyName1, propertyName2);
        }
        public AbstractCriterion NotEqProperty(ICriterion expression)
        {
            if (ObjectUtil.IsNotEmpty(expression))
            {
                return Restrictions.Not(expression);
            }
            return null;
        }
        public AbstractCriterion Like(string propertyName, object value)
        {
            if (ObjectUtil.IsNotEmpty(value))
            {
                return Restrictions.Like(propertyName, value);
            }
            return null;
        }
        public AbstractEmptinessExpression IsEmpty(string propertyName)
        {
            return Restrictions.IsEmpty(propertyName);
        }
        public AbstractCriterion In(string propertyName, object[] values)
        {
            if (ObjectUtil.IsNotEmpty(values) && values.Count() > 0)
            {
                return Restrictions.In(propertyName, values);
            }
            return null;
        }
    }
}
